﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao.CommonDao;
using CSP.Model.CSP;
using CSP.DataAccess;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class UserBasicDao : CSPCommonDao, IUserBasicDao
    {
        public UserBasicDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public ICollection<Model.CSP.UserBasic> FindAll()
        {
            return _context.UserBasics.ToArray();
        }

        public UserBasic GetInfoByUserName(string userName)
        {
            return _context.UserBasics.FirstOrDefault(a => a.username == userName);
        }


        public Model.CSP.UserBasic GetDataById(int id)
        {
            return _context.UserBasics.FirstOrDefault(a => a.id == id);
        }

        public ICollection<Model.CSP.UserBasic> GeDatatByIds(int[] Ids)
        {
            return _context.UserBasics.Where(a => Ids.Contains(a.id)).ToArray();
        }

        public void Add(Model.CSP.UserBasic obj)
        {
            _context.UserBasics.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.UserBasic obj)
        {
            UserBasic userBasic = GetDataById(obj.id);
            if (userBasic != null)
            {
                userBasic = obj;
            }
            _context.SaveChanges();
        }

        public UserBasic DeleteById(int id)
        {
            UserBasic userBasic = GetDataById(id);
            _context.UserBasics.Remove(userBasic);
            _context.SaveChanges();

            return userBasic;
        }

    }
}
